const { app, BrowserWindow,Menu  } = require('electron');
const path = require('path');

function createWindow() {
  const win = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'), // 可选
      nodeIntegration: false,
      contextIsolation: true
    }
  });

  // 开发时用 localhost，打包后用 file://
  const isDev = process.env.NODE_ENV === 'development';
  win.loadURL(isDev ? 'http://localhost:7982' : `file://${path.join(__dirname, 'index.html')}`);
}

/*------------------------------------------------*/
/*  关键：自定义菜单  */
/*------------------------------------------------*/
const isMac = process.platform === 'darwin';   // macOS 需要把第一个菜单写成 app 名
const template = [
  // ① 应用 / File
  {
    label: isMac ? app.getName() : '文件（F）',
    submenu: [
      { label: '新建项目', accelerator: 'CmdOrCtrl+N', click: () => console.log('新建') },
      { label: '打开', accelerator: 'CmdOrCtrl+O', click: () => console.log('打开') },
      { type: 'separator' },
      { label: '退出', accelerator: isMac ? 'Cmd+Q' : 'Ctrl+Q', click: () => app.quit() }
    ]
  },
  // ② 编辑
  {
    label: '编辑（E）',
    submenu: [
      { role: 'undo', label: '撤销' },
      { role: 'redo', label: '重做' },
      { type: 'separator' },
      { role: 'cut', label: '剪切' },
      { role: 'copy', label: '复制' },
      { role: 'paste', label: '粘贴' }
    ]
  },
  // ③ 视图
  {
    label: '视图（V）',
    submenu: [
      { role: 'reload', label: '重载' },
      { role: 'forceReload', label: '强制重载' },
      { type: 'separator' },
      { role: 'resetzoom', label: '重置缩放' },
      { role: 'zoomin', label: '放大' },
      { role: 'zoomout', label: '缩小' },
      { type: 'separator' },
      { role: 'togglefullscreen', label: '全屏' }
    ]
  },
  // ④ 窗口
  {
    label: '窗口（W）',
    submenu: [
      { role: 'minimize', label: '最小化' },
      { role: 'close', label: '关闭' }
    ]
  },
  // ⑤ 帮助
  {
    label: '帮助（H）',
    submenu: [
      {
        label: '关于 lq',
        click: async () => {
          const { dialog } = require('electron');
          dialog.showMessageBox({
            type: 'info',
            title: '关于',
            message: `lq今年18岁，人高、帅、穷`
          });
        }
      }
    ]
  }
];

// 生成菜单并应用
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

app.whenReady().then(()=>{
    createWindow();
});
app.on('window-all-closed', () => app.quit());